﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework;

namespace MiaProject
{
    public class Sound
    {
        public AudioEngine audioEngine;
        public WaveBank waveBank;
        public SoundBank soundBank;
        public Cue engineSound;
        public AudioCategory musicCategory;
        public float musicVolume = 1.0f;
        public List<SoundEffect> soundList;
        public List<String> soundListFile;
        private static Sound instance;

        public static Sound getInstance()
        {
            if (instance == null)
                instance = new Sound();
            return instance;
        }

        private Sound()
        {
            soundList = new List<SoundEffect>();
            soundListFile = new List<String>();

            engineSound = null;

            audioEngine = new AudioEngine("Content\\sound\\MiaProjectGameAudio.xgs");
            waveBank = new WaveBank(audioEngine, "Content\\sound\\Wave Bank.xwb");
            soundBank = new SoundBank(audioEngine, "Content\\sound\\Sound Bank.xsb");

            if (engineSound == null)
            {
                engineSound = soundBank.GetCue("music");
                musicCategory = audioEngine.GetCategory("Music");
                engineSound.Play();
            }
        }

        public void pauseResumeMusic()
        {
            if (engineSound != null && engineSound.IsPaused)
            {
                engineSound.Resume();
            }
            else if (engineSound != null && engineSound.IsPlaying)
                engineSound.Pause();

        }
        public void setGameVolume()
        {
            musicVolume = MathHelper.Clamp(0.3f, 0.0f, 2.0f);
            musicCategory.SetVolume(musicVolume);

        }
        public void setMenuVolume()
        {
            musicVolume = MathHelper.Clamp(1.0f, 0.0f, 2.0f);
            musicCategory.SetVolume(musicVolume);

        }
        /**
            Verifica se o sound ja nao foi carregado, caso sim utiliza
            senao carrega e add a lista
        */
        /*public SoundEffect getSound(String file,ContentMa)
        {
            //verifica se ja nao foi carregado
            int i = 0;
            foreach (String f in soundListFile)
            {
                if (f.Equals(file))
                    return (SoundEffect)soundList[i];
                i++;
            }

            SoundEffect m = content.Load<SoundEffect>(file);

            soundList.Add(m);
            soundListFile.Add(file);

            return m;
        }//end of method
         * */
        //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

    }
}
